package com.bj58.ecdata.calc.dbservice.service.impl;

import java.util.List;
import java.util.Map;

import com.bj58.ecdata.calc.dbservice.consts.Constant;
import com.bj58.ecdata.calc.dbservice.dao.DisplayDao;
import com.bj58.ecdata.calc.dbservice.dao.model.DataPoint;
import com.bj58.ecdata.calc.dbservice.dao.model.DisplayModel;
import com.bj58.ecdata.calc.dbservice.load.SpringUtils;
import com.bj58.ecdata.calc.dbservice.service.BusslineEnum;
import com.bj58.ecdata.calc.dbservice.service.DisplayService;
import com.bj58.ecdata.calc.dbservice.service.PlatformEnum;
import com.bj58.ecdata.util.DateUtils;
import com.bj58.ecdata.util.JsonUtils;
import com.bj58.spat.scf.server.contract.annotation.ServiceBehavior;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author lichao
 *
 */
@ServiceBehavior
public class DisplayServiceImpl extends BaseServiceImpl implements DisplayService {

	public DisplayServiceImpl() {
		super(Lists.newArrayList("pv", "ad", "adNum"));
	}

	private DisplayDao dao = SpringUtils.getInstance().getBean(DisplayDao.class);

	@Override
	public String getData(String type, PlatformEnum platform, BusslineEnum bussline, long time) {

		checkType(type);

		long endTime = dao.getLatest();
		List<DisplayModel> params = createModels(time, endTime, Constant._2s, Constant._1h, platform, bussline);
		List<DataPoint> datas = dao.load(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	@Override
	public String getDataTodayHourly(String type, PlatformEnum platform, BusslineEnum bussline) {

		checkType(type);

		long zeroTime = DateUtils.getTruncatedToday();
		long curTime = dao.getLatest();
		long endTime = DateUtils.truncateToHour(curTime) - Constant._1h;
		List<DisplayModel> params = createModels(0, endTime, Constant._1h, endTime - zeroTime + Constant._1h, platform, bussline);
		List<DataPoint> datas = dao.loadHourly(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	@Override
	public String getDataYesterdayHourly(String type, PlatformEnum platform, BusslineEnum bussline) {

		checkType(type);

		long endTime = DateUtils.getTruncatedToday() - Constant._1h;
		List<DisplayModel> params = createModels(0, endTime, Constant._1h, Constant._1d, platform, bussline);
		List<DataPoint> datas = dao.loadHourly(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	private List<DisplayModel> createModels(long startTime, long endTime, long interval, long span, PlatformEnum platform, BusslineEnum bussline) {
		long minTime = endTime - span;
		if (startTime == 0 || startTime < minTime) {
			startTime = minTime;
		}

		List<DisplayModel> models = Lists.newArrayList();
		for (long i = startTime + interval; i <= endTime; i += interval) {

			DisplayModel model = new DisplayModel();
			model.setTime(i);
			model.setPlatform(platform != null ? platform.value() : null);
			model.setBussline(bussline != null ? bussline.value() : null);
			models.add(model);
		}
		return models;
	}

}
